/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary.management;

import components.TabPanel;
import components.utils.ComponentGroup;
import components.utils.ListSelectionEnabler;
import components.utils.observer.ListUpdater;
import control.Application;
import entity.Employee;
import entity.Entity;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan
 */
public class PEmployeeManagment extends TabPanel {
    
    private ComponentGroup listControls = new ComponentGroup();
    private ListUpdater employeeUpdater;

    /**
     * Creates new form PEmployeeManagment
     */
    public PEmployeeManagment() {
        initComponents();
        
        listControls.addComponent(BMoreInfo);
        listControls.addComponent(BDeleteEmployee);
        
        LEmployees.addListSelectionListener(new ListSelectionEnabler(listControls, LEmployees));
        
        employeeUpdater =
                new ListUpdater(Application.EMPLOYEE_MANAGER.getCollection(), LEmployees);
        Application.EMPLOYEE_MANAGER.addObserver(employeeUpdater);
        employeeUpdater.notifyObserver();
    }

    @Override
    protected void removeObservers() {
        Application.EMPLOYEE_MANAGER.removeObserver(employeeUpdater);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        LEmployees = new javax.swing.JList();
        BMoreInfo = new javax.swing.JButton();
        BRegister = new javax.swing.JButton();
        BClose = new javax.swing.JButton();
        BDeleteEmployee = new javax.swing.JButton();

        jLabel1.setText("Empleados registrados:");

        LEmployees.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        LEmployees.setToolTipText("Haga doble clic en el nombre de un empleado para ver su información.");
        LEmployees.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LEmployeesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LEmployees);

        BMoreInfo.setText("Ver Información");
        BMoreInfo.setEnabled(false);
        BMoreInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BMoreInfoActionPerformed(evt);
            }
        });

        BRegister.setText("Registrar Nuevo Empleado");
        BRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BRegisterActionPerformed(evt);
            }
        });

        BClose.setText("Cerrar");
        BClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCloseActionPerformed(evt);
            }
        });

        BDeleteEmployee.setText("Borrar Empleado");
        BDeleteEmployee.setEnabled(false);
        BDeleteEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDeleteEmployeeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(BRegister)
                            .addComponent(BClose)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BMoreInfo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BDeleteEmployee)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BMoreInfo)
                    .addComponent(BDeleteEmployee))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BRegister)
                .addGap(18, 18, 18)
                .addComponent(BClose)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCloseActionPerformed
        buttonTabComponent.closeTab();
    }//GEN-LAST:event_BCloseActionPerformed

    private void BRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BRegisterActionPerformed
        Application.F_MAIN.openEmployeeRegistrationPanel();
    }//GEN-LAST:event_BRegisterActionPerformed

    private void BDeleteEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDeleteEmployeeActionPerformed
        int selectedIndex = LEmployees.getSelectedIndex();
        if (selectedIndex != -1) {
            Employee employee = (Employee) LEmployees.getSelectedValue();
            int choice = JOptionPane.showConfirmDialog(this.getTopLevelAncestor(),
                                            "¿Está seguro que desea eliminar el empleado " + employee + "?",
                                            "Advertencia",
                                            JOptionPane.YES_NO_OPTION,
                                            JOptionPane.WARNING_MESSAGE);
            if (choice == JOptionPane.YES_OPTION) {
                DefaultListModel model = (DefaultListModel) LEmployees.getModel();
                model.removeElement(employee);
                if (selectedIndex == 0) {
                    if (model.getSize() == 0) {
                        LEmployees.setSelectedIndex(-1);
                    } else {
                        LEmployees.setSelectedIndex(0);
                    }
                } else {
                    LEmployees.setSelectedIndex(--selectedIndex);
                }
                Application.EMPLOYEE_MANAGER.removeObject(employee);
            }
        }
    }//GEN-LAST:event_BDeleteEmployeeActionPerformed

    private void LEmployeesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LEmployeesMouseClicked
        Employee selectedEmployee = (Employee) LEmployees.getSelectedValue();
        if (evt.getClickCount() == 2 && selectedEmployee != null) {
            Application.displayEntity(selectedEmployee);
        }
    }//GEN-LAST:event_LEmployeesMouseClicked

    private void BMoreInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BMoreInfoActionPerformed
        int selectedIndex = LEmployees.getSelectedIndex();
        if (selectedIndex != -1) {
            Application.displayEntity((Entity) LEmployees.getSelectedValue());
        }
    }//GEN-LAST:event_BMoreInfoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BClose;
    private javax.swing.JButton BDeleteEmployee;
    private javax.swing.JButton BMoreInfo;
    private javax.swing.JButton BRegister;
    private javax.swing.JList LEmployees;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
